package com.inossem.warehousestorage_inossem.adapter.inventory.chuangyou;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.inventory.chuangyou.InventoryMatDetailsActivity;
import com.inossem.warehousestorage_inossem.activity.inventory.chuangyou.InventorySearchMatActivity;
import com.inossem.warehousestorage_inossem.activity.inventory.extre.InventoryExtre;
import com.inossem.warehousestorage_inossem.bean.inventory.chuangyou.InventorySearchMatBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;

import java.util.List;

/**
 * TODO
 *
 * @author 詹建宇
 * @time on 2018/4/2 15:02
 * @email jianyu.zhan@inossem.com
 */
public class InventorySearchMatAdapter extends BaseAdapter {

    private Activity context;
    private List<ItemListBean> list;
    private String mCondition;
    private InventorySearchMatActivity.SearchListener listener;

    public InventorySearchMatAdapter(Activity context, List<ItemListBean> list, String mCondition, InventorySearchMatActivity.SearchListener listener) {
        this.context = context;
        this.list = list;
        this.mCondition = mCondition;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = LayoutInflater.from(context).inflate(R.layout.view_out_storage_picking_list_first_item, null);
            EditText condition = (EditText) convertView.findViewById(R.id.condition);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            condition.setText(mCondition);
            condition.setHint("请输入物料信息");
            search.setOnClickListener(listener.getNoDoubleClickListener(condition));
        } else {
            ItemListBean bean = list.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.cy_view_inventory_material_search, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(context, viewHolder, convertView, bean);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.codeTv = (TextView) view.findViewById(R.id.codeTv);
        viewHolder.describeTv = (TextView) view.findViewById(R.id.describeTv);
    }

    private void initData(Activity context, ViewHolder viewHolder, View view, ItemListBean bean) {
        viewHolder.codeTv.setText(bean.getMat_code());
        viewHolder.describeTv.setText(bean.getMat_name());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(InventoryExtre.SEARCH_MAT_BODY_BEAN, bean);
                context.setResult(InventoryMatDetailsActivity.RESULT_MAT, intent);
                context.finish();
            }
        });
    }

    private class ViewHolder {
        private TextView codeTv;
        private TextView describeTv;
    }
}
